package com.zjdiepu.www.biz.dh;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.dh.DhOverdueListMapper;
import com.zjdiepu.www.ibiz.dh.IDhOverdueListService;
import com.zjdiepu.www.model.dh.dto.DhOverdueList;
import com.zjdiepu.www.model.dh.ext.DhOverdueListExt;

/**
 * idService层操作实现<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年06月07日 17时48分<br/>
 * @since 1.0<br/>
 */
@Service
public class DhOverdueListServiceImpl extends BaseService implements IDhOverdueListService {
	public static final Logger logger = LoggerFactory.getLogger(DhOverdueListServiceImpl.class);

	@Autowired
	private DhOverdueListMapper dhOverdueListMapper;
	
	@Override
	public void saveUpdateAndDelete(DhOverdueList dhOverdueList, OperateType operType) {
		switch (operType) {
		case add:
			dhOverdueList.initInsertData(dhOverdueList.getModifier(), dhOverdueList.getModifierName());
			dhOverdueListMapper.insert(dhOverdueList);
			break;
		case edit:
			dhOverdueList.initUpdateData(dhOverdueList.getModifier(), dhOverdueList.getModifierName());
//			try {
//				dhOverdueList.setModified(ZhppDateUtil.dateConvertTimeStamp(dhOverdueList.getJbDate(), "yyyy-MM-dd"));
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
			// 影响的记录数
			int updateCount = dhOverdueListMapper.updateByPrimaryKeySelective(dhOverdueList);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			dhOverdueList.initDeleteData(dhOverdueList.getModifier(), dhOverdueList.getModifierName());
			// 影响的记录数
			int deleteCount = dhOverdueListMapper.updateByPrimaryKeySelective(dhOverdueList);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<DhOverdueList> selectByExt(DhOverdueListExt dhOverdueListExt) {
		return dhOverdueListMapper.selectByExt(dhOverdueListExt);
	}
	
	@Override
	public int countByExt(DhOverdueListExt dhOverdueListExt){
		return dhOverdueListMapper.countByExt(dhOverdueListExt);
	}
	
	@Override
	public DhOverdueList selectById(String id) {
		return dhOverdueListMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			dhOverdueListMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(DhOverdueList dhOverdueList) {
		return dhOverdueListMapper.countRefined(dhOverdueList);
	}

	@Override
	public DhOverdueList selectRefined(DhOverdueList dhOverdueList) {
		return dhOverdueListMapper.selectRefined(dhOverdueList);
	}

	@Override
	public boolean repeat(DhOverdueList dhOverdueList) {
		int count = countRefined(dhOverdueList);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(dhOverdueList.getId())) {
			return true;
		}
		DhOverdueList dbItem = selectRefined(dhOverdueList);
		if(null == dbItem) {
			return false;
		}
		return !dhOverdueList.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dhOverdueListMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}

	@Override
	public int updateDispatchBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids, 
			String idOperatorPhone, String nameOperatorPhone) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return dhOverdueListMapper.updateDispatchBatch(modifier, modifierName, current, dpStatus, ids,idOperatorPhone,nameOperatorPhone);
		
	}
}
